﻿// @include "Namespace.js"
// @include "Typs.js"
// @include "Extend.js"
// @include "Function.js"
// @include "Argument.js"
/**
 * @fileOverview 类声明
 * 
 * @version qinglan_Class 1.0
 * @static
 * @author wxq wxqqh@qq.com
 */
/**
 * 命名空间
 * 
 * @namespace
 * @type Object
 */
window.qinglan = window.qinglan || {};
/**
 * 类工具命名空间
 * 
 * @namespace
 * @type Object
 */
qinglan.Class = qinglan.Class || {};
/**
 * 创建一个类<br/> 可以独立使用
 * 
 * @param {Object}
 *            baseClass 基类
 * @param {Object}
 *            extend 扩展类
 * @param {Boolean}
 *            staticMethod 是否使用静态方法扩展
 * @return {Object} 扩展后的类
 * @function
 * @static
 * @public
 */
qinglan.Class.create = function(baseClass, extend, staticMethod) {
	var klass = baseClass ? baseClass.extend({}, true) : function() {
	};
	var ext = extend || {};
	if (baseClass) {
		klass = baseClass.extend({}, true);
	} else {
		klass = function() {
		};
		klass.extend = function(ext, createNewClass) {
			var target = this;
			if (createNewClass) {
				target = arguments.callee.call(function() {
						}, this.prototype);
				target.extend = this.extend;
				target.create = this.create;
			}
			for (var i in ext) {
				target.prototype[i] = ext[i];
			}
			return target;
		};
		klass.create = ext.init || function() {
			return new this();
		};
	}
	if (staticMethod) {
		for (var i in ext) {
			klass[i] = ext[i];
		}
	} else {
		klass.extend(ext);
	}
	return klass;
};
/**
 * 创建模块应用
 * 
 * @param {String}
 *            nameSpace 命名空间
 * @param {Object}{Function}
 *            prototype 对象的类成员属性
 * @param {Object}
 *            staticMod 对象的静态类方法
 * @param {Object}
 *            context 上下文 所生命的命名空间将会挂在这个上下文里面,默认是window
 * @return {Object} 模块的类
 * @function
 * @static
 * @public
 */
qinglan.Class.module = function(nameSpace, prototypes, staticMod, context) {
	/**
	 * 得到prototype的对象
	 */
	function firePrototype(proto) {
		return qinglan.Type.isFunction(proto) ? proto.call() : proto;
	}

	// 扩展默认的constructor和init函数
	prototypes = firePrototype(prototypes);
	prototypes = qinglan.Extend.simple({
				constructor : qinglan.Function.emptyFun,
				init : qinglan.Function.emptyFun
			}, prototypes, 1);

	var ns = qinglan.Namespace.create(nameSpace, context,true);
	!ns.instance[ns.key] && (ns.instance[ns.key] = {});
	!ns.instance[ns.key]._constructor
			&& (ns.instance[ns.key]._constructor = prototypes.constructor);
	!ns.instance[ns.key]._init && (ns.instance[ns.key]._init = prototypes.init);
	delete prototypes.constructor;
	delete prototypes.init;
	ns.instance[ns.key] = (function(self) {
		var that = self;
		function _init() {
			that._constructor.apply(this, arguments);
			that._init.apply(this, arguments);
		}
		return function() {
			_init.apply(this, arguments);
		};
	})(ns.instance[ns.key]);
	qinglan.Extend.simple(ns.instance[ns.key], prototypes, false);
	qinglan.Extend.simple(ns.instance[ns.key], firePrototype(staticMod), true);
	return ns.instance[ns.key];
};